<?php


namespace app\Validate;



use app\lib\exception\ParameterException;
use Cassandra\Exception\ProtocolException;
use think\facade\Request;
use think\Validate;


class BaseValidate extends Validate
{

//    /**
//     * 默认规则提示
//     * @var array
//     */
//    protected $typeMsg = [
//        'require'     => ':attribute require',
//        'must'        => ':attribute must',
//        'number'      => ':attribute must be numeric',
//        'integer'     => ':attribute must be integer',
//        'float'       => ':attribute must be float',
//        'boolean'     => ':attribute must be bool',
//        'email'       => ':attribute not a valid email address',
//        'mobile'      => ':attribute not a valid mobile',
//        'array'       => ':attribute must be a array',
//        'accepted'    => ':attribute must be yes,on or 1',
//        'date'        => ':attribute not a valid datetime',
//        'file'        => ':attribute not a valid file',
//        'image'       => ':attribute not a valid image',
//        'alpha'       => ':attribute must be alpha',
//        'alphaNum'    => ':attribute must be alpha-numeric',
//        'alphaDash'   => ':attribute must be alpha-numeric, dash, underscore',
//        'activeUrl'   => ':attribute not a valid domain or ip',
//        'chs'         => ':attribute must be chinese',
//        'chsAlpha'    => ':attribute must be chinese or alpha',
//        'chsAlphaNum' => ':attribute must be chinese,alpha-numeric',
//        'chsDash'     => ':attribute must be chinese,alpha-numeric,underscore, dash',
//        'url'         => ':attribute not a valid url',
//        'ip'          => ':attribute not a valid ip',
//        'dateFormat'  => ':attribute must be dateFormat of :rule',
//        'in'          => ':attribute must be in :rule',
//        'notIn'       => ':attribute be notin :rule',
//        'between'     => ':attribute must between :1 - :2',
//        'notBetween'  => ':attribute not between :1 - :2',
//        'length'      => 'size of :attribute must be :rule',
//        'max'         => 'max size of :attribute must be :rule',
//        'min'         => 'min size of :attribute must be :rule',
//        'after'       => ':attribute cannot be less than :rule',
//        'before'      => ':attribute cannot exceed :rule',
//        'expire'      => ':attribute not within :rule',
//        'allowIp'     => 'access IP is not allowed',
//        'denyIp'      => 'access IP denied',
//        'confirm'     => ':attribute out of accord with :2',
//        'different'   => ':attribute cannot be same with :2',
//        'egt'         => ':attribute must greater than or equal :rule',
//        'gt'          => ':attribute must greater than :rule',
//        'elt'         => ':attribute must less than or equal :rule',
//        'lt'          => ':attribute must less than :rule',
//        'eq'          => ':attribute must equal :rule',
//        'unique'      => ':attribute has exists',
//        'regex'       => ':attribute not conform to the rules',
//        'method'      => 'invalid Request method',
//        'token'       => 'invalid token',
//        'fileSize'    => 'filesize not match',
//        'fileExt'     => 'extensions to upload is not allowed',
//        'fileMime'    => 'mimetype to upload is not allowed',
//    ];




    public function goCheck(){

        $params = request()->param();
        $result = $this->batch()->check($params);

        if (!$result) {
            return false;
        } else {
            return true;
        }

    }
    /**
     * Verification ID
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return bool
     */
    public function isPositiveInteger($value, $rule = '', $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Verification is Empty
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return bool
     */
    public function isNotEmpty($value, $rule = '', $data = '', $field = '')
    {
        if (empty($value) || $value === "") {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Verification is Mobile
     * @param $value
     * @return bool
     */
    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Verification is Mobile
     * @param $value
     * @return bool
     */
    protected function isIp($value)
    {
        $rule = '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param array $arrays request.post
     * @return array Array of variables filtered according to rule key
     * @throws ParameterException
     */
    public function getDataByRule($arrays)
    {
        if (array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays)) {
            throw new ProtocolException([],1);
        }
        $newArray = [];
        foreach ($this->rule as $key => $value) {
            $newArray[$key] = $arrays[$key];
        }
        return $newArray;
    }




}
